﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Windows.Media;
using Microsoft.Phone.Maps.Controls;

namespace YellowMap.View.Control
{
    public partial class YellowMapPin : UserControl
    {
        public MapOverlay MapOverlay { get; set; }
        public YellowMapPin()
        {
            InitializeComponent();
            LayoutRoot.Tap += LayoutRoot_Tap;
            OnShowDetail += YellowMapPin_OnShowDetail;
            path.Tap += path_Tap;
            full.Tap += path_Tap;
            ShowIcon();
        }

        void path_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            OnShowDetail(Place);
        }

        void YellowMapPin_OnShowDetail(Place obj)
        {
            
        }

        public event Action<Place> OnShowDetail;

        void LayoutRoot_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            //OnShowDetail(Place);
            MainPage.Instance.LayerPushPin.Remove(MapOverlay);
            MainPage.Instance.LayerPushPin.Add(MapOverlay);
        }

        Place place;
        public Place Place {
            get {
                return place;
            }

            set {
                place = value;
                txtName.Text = value.Name;
            }
        }

        public void Init(Place place, string color) {
            this.Place = place;
            //path.Fill = new SolidColorBrush(parseColor(color));
        }

        static Color parseColor(string hexaColor)
        {
            hexaColor = hexaColor.Trim(new char[] { '#' });
            if (hexaColor.Length == 6)
            {
                hexaColor += "ff";
            }

            return Color.FromArgb(
                    Convert.ToByte(hexaColor.Substring(6, 2), 16),
                    Convert.ToByte(hexaColor.Substring(0, 2), 16),
                    Convert.ToByte(hexaColor.Substring(2, 2), 16),
                    Convert.ToByte(hexaColor.Substring(4, 2), 16));
        }


        public void ShowIcon()
        {
            full.Visibility = System.Windows.Visibility.Visible;
        }

        public void ShowFull() 
        {
            full.Visibility = System.Windows.Visibility.Visible;
        }

        private void border_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
        	// TODO: Add event handler implementation here.
        }


    }
}
